package com.gmail.dmitryustimov.ms.java7.visitor.mod_reflective.example.example1;

/**
 * Author: Dmitry Ustimov
 * Date: 5/10/13
 * Time: 10:34 AM
 */
public class CalculationVisitor extends Visitor {

    public int getResult() { return result; }
    protected int result;

    protected void evaluate(AddExpr expr) throws NoSuchMethodException {
        Expression left = expr.getLeft();
        Expression right = expr.getRight();
        visit(left);
        int leftResult = result;
        visit(right);
        result = leftResult + result;
    }
    protected void evaluate(SubExpr expr) throws NoSuchMethodException {
        Expression left = expr.getLeft();
        Expression right = expr.getRight();
        visit(left);
        int leftResult = result;
        visit(right);
        result = leftResult - result;
    }
    protected void evaluate(MulExpr expr) throws NoSuchMethodException {
        Expression left = expr.getLeft();
        Expression right = expr.getRight();
        visit(left);
        int leftResult = result;
        visit(right);
        result = leftResult * result;
    }
    protected void evaluate(DivExpr expr) throws NoSuchMethodException {
        Expression left = expr.getLeft();
        Expression right = expr.getRight();
        visit(left);
        int leftResult = result;
        visit(right);
        result = leftResult / result;
    }
    protected void evaluate(Constant c) {
        result = c.getValue();
    }
}
